package com.rxgscd.yy.wxapi;

import com.sdk.API;
import com.sdk.SDKUtility;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
//import com.umeng.analytics.game.UMGameAgent;
import com.rxgscd.yy.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	//private static final String APP_ID = "wx15d244c8734a33f0";
	// IWXAPI 是第三方app和微信通信的openapi接口
	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 通过WXAPIFactory工厂，获取IWXAPI的实例
		api = WXAPIFactory.createWXAPI(this, MainActivity.APP_ID, true);
		api.registerApp(MainActivity.APP_ID);

		try {
			Log.i("UNITY", "微信支付activity注冊成功");
			api.handleIntent(getIntent(), (IWXAPIEventHandler) this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onReq(BaseReq arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResp(BaseResp arg0) {
		Log.i("UNITY",
				"微信支付返回 == " + arg0 + "                返回的命令内容为==" + arg0.getType() + "     返回的错误信息是：" + arg0.errStr);
		if (arg0.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Log.i("UNITY", "微信返回的支付结果Code == " + arg0.errCode);
			SDKUtility.SendMessage(SDKUtility.CALLBACKFUNCTIONNAME.PAYCALLBACK, arg0.errCode + "");
			if (arg0.errCode == 0) {
				//UMGameAgent.exchange(API.MoneyCount, "元宝", API.DiamondCount, SDKUtility.Channels.WeiXin, "");
			}
		}
		finish();
	}
}
